package yang.zhan.zhao.datatransferdemo.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.logging.Logger;


public class AESUtils {
	private static final Logger log = Logger.getLogger(AESUtils.class.getName());

	public static String Encrypt(String sSrc, String sKey) throws Exception {
		byte[] raw = sKey.getBytes("utf-8");
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(1, skeySpec);
		byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
		return Base64.getEncoder().encodeToString(encrypted);
	}

	public static String Decrypt(String sSrc, String sKey) {
		try {
			if (sKey == null) {
				log.info("key isNull");
				return null;
			} else {
				byte[] raw = sKey.getBytes("utf-8");
				SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
				Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
				cipher.init(2, skeySpec);
				byte[] encrypted1 =  Base64.getDecoder().decode(sSrc);

				try {
					byte[] original = cipher.doFinal(encrypted1);
					String originalString = new String(original, "utf-8");
					return originalString;
				} catch (Exception var8) {
					log.info(var8.toString());
					return null;
				}
			}
		} catch (Exception var9) {
			log.info(var9.toString());
			return null;
		}
	}


}
